package cn.itcast.nems.enrol.converter;

import cn.itcast.nems.common.constant.PaymentTypeEum;
import cn.itcast.nems.common.utils.BigDecimalUtil;
import cn.itcast.nems.enrol.command.EnrolQuickCmd;
import cn.itcast.nems.enrol.dto.*;
import cn.itcast.nems.enrol.service.impl.ActivityConfig;
import cn.itcast.nems.manager.scrm.dto.CollegeClue;
import cn.itcast.nems.manager.scrm.dto.CollegeClueAddResult;
import cn.itcast.nems.product.dto.ProductDTO;
import cn.itcast.nems.studentrecord.enumeration.TeachingModelEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

/**
 * author liyong
 * create: 2024-04-14 10:36:17
 **/
public class EnrolConverter {

    public static EnrolQuickFormDTO comandToQuickForm(CollegeClueAddResult ccAddResult,
                                                      ActivityCourseDTO course,
                                                      Integer tuitionFeeId,
                                                      ProductDTO product,
                                                      List<EnrolDiscountDTO> discountList,
                                                      String bizOrderNo) {
        EnrolQuickFormDTO result = new EnrolQuickFormDTO();
        result.setCreateVO(enrolCreateVO(ccAddResult));
        final EnrolOrderDTO order = enrolOrderDTO(ccAddResult,
                course,
                product,
                discountList,
                bizOrderNo);
        order.setTuitionFeeId(tuitionFeeId);
        order.setPersonalDiscountList(Collections.emptyList());
        result.setOrder(order);
        return result;
    }

    private static EnrolOrderDTO enrolOrderDTO(CollegeClueAddResult ccAddResult,
                                               ActivityCourseDTO course,
                                               ProductDTO product,
                                               List<EnrolDiscountDTO> discountList,
                                               String bizOrderNo) {
        EnrolOrderDTO order = new EnrolOrderDTO();
        order.setCustomer(enrolCustomerDTO(ccAddResult));
        order.setClassId(course.getClazzId());
        order.setPhaseNumber(1);
        order.setBizOrderNo(bizOrderNo);
        BigDecimal discountAmount = BigDecimal.ZERO;
        if (discountList != null && !discountList.isEmpty()) {
            discountAmount = discountList.stream().map(EnrolDiscountDTO::getDiscountMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        EnrolOrderLineDTO line = enrolOrderLineDTO(product, discountAmount, course);
        order.setOrderLineList(List.of(line));
        order.setPaymentTypeCode(PaymentTypeEum.FIRSTPAYMENT.name());
        order.setOriginChannelCode(ccAddResult.getOriginChannelCode());
        order.setBusinessLineCode(ccAddResult.getBusinessLineCode());
        order.setDiscountList(discountList);
        order.setIsacademy(true);
        order.setIsJoinClass(false);
        if (course.getJobPrice() != null) {
            BigDecimal jobFee = BigDecimalUtil.centIntToBigDecimal(course.getJobPrice());
            order.setJobFee(jobFee);
            final String jobTeachingModel = course.getJobTeachingModel();
            order.setJobTeachingModel(TeachingModelEnum.FACETOFACE.name());
            if (StringUtils.hasText(jobTeachingModel)) {
                order.setJobTeachingModel(jobTeachingModel);
            }
        }
        return order;
    }

    private static EnrolOrderLineDTO enrolOrderLineDTO(ProductDTO product,
                                                       BigDecimal discountAmount,
                                                       ActivityCourseDTO course) {
        EnrolOrderLineDTO line = new EnrolOrderLineDTO();
        line.setProductId(product.getId());
        line.setQuantity(Byte.valueOf("1"));
        line.setSchoolId(product.getCampusId());
        line.setModelId(product.getModelId());
        line.setSubjectId(product.getSubjectId());
        line.setClassTypeId(product.getClassTypeId());
        BigDecimal price = BigDecimalUtil.centIntToBigDecimal(product.getPrice());
        line.setPrice(price);
        line.setAmountDue(price.subtract(discountAmount));
        line.setDiscountAccount(discountAmount);
        final BigDecimal firstPay = BigDecimalUtil.centIntToBigDecimal(course.getFirstPayAmount());
        line.setFirstPayment(firstPay);
        return line;
    }

    private static EnrolCustomerDTO enrolCustomerDTO(CollegeClueAddResult ccAddResult) {
        EnrolCustomerDTO customer = new EnrolCustomerDTO();
        BeanUtils.copyProperties(ccAddResult, customer);
        return customer;
    }

    private static EnrolCreateVO enrolCreateVO(CollegeClueAddResult ccAddResult) {
        EnrolCreateVO result = new EnrolCreateVO();
        BeanUtils.copyProperties(ccAddResult, result);
        return result;
    }

    public static CollegeClue comandToClue(EnrolQuickCmd cmd, ActivityConfig config) {
        CollegeClue cc = new CollegeClue();
        cc.setCustomerName(cmd.getName());
        cc.setCustomerPhone(cmd.getMobile());
        cc.setOperatorEmail(config.getConsultantEmail());
        cc.setCustomerGender("MAN");
        cc.setActivityId(config.getActivityId());
        cc.setProjectId(config.getProjectId());
        return cc;
    }
}
